﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FGK
{
    class Kolor
    {
        private float r;
        private float g;
        private float b;
        private float a;

        #region setters&getters

        public float R
        {
            get { return r; }
            set { r = value; }
        }
        public float G
        {
            get { return g; }
            set { g = value; }
        }
        public float B
        {
            get { return b; }
            set { b = value; }
        }
        public float A
        {
            get { return a; }
            set { a = value; }
        }

        #endregion setters&getters

        public Kolor(float _r, float _g, float _b, float _a)
        {
            if (_r < 0.0f || _g < 0.0f || _b < 0.0f || _a < 0f
              || _r > 1.0f || _g > 1.0f || _b > 1.0f || _a > 0.0f)
            {
                throw new Exception("Jedna ze składowych koloru poza zakresem 0.0-1.0");
            }
            this.r = _r;
            this.g = _g;
            this.b = _b;
            this.a = _a;
        }

        public Kolor(float _r, float _g, float _b)
        {
           if ( _r > 1.0f) { _r = 1.0f; }
           if (_r < 0.0f) { _r = 0.0f; }
            if (_g > 1.0f) { _g = 1.0f; }
            if (_g < 0.0f) { _g = 0.0f; }
            if (_b > 1.0f) { _b = 1.0f; }
            if (_b < 0.0f) { _b = 0.0f; }
                      
                this.r = _r;
                this.g = _g;
                this.b = _b;
                this.a = 1.0f;
         
        }

        public Kolor() // domyślny konstruktor - kolor czarny
        {
            this.r = 0.0f;
            this.g = 0.0f;
            this.b = 0.0f;
            this.a = 1.0f;
        }

        public Kolor(uint color)
        {
           this.r = (byte)((color & 0xff0000) >> 16);
           this.g = (byte)((color & 0x00ff00) >> 8);
           this.b = (byte)((color & 0x0000ff) >> 0);
           r = (float)(r / 255);
           g = (float)(g / 255);
           b = (float)(b / 255);
           r = (float) Math.Round(r, 2);
           g = (float) Math.Round(g, 2);
           b = (float) Math.Round(b, 2);
           a = 1.0f;
        }

        public void zmienJasnosc(float value)
        {
            this.r -= value;
            this.g -= value;
            this.b -= value;
            if (r < 0.0f) r = 0.0f;
            if (r > 1.0f) r = 1.0f;
            if (g < 0.0f) g = 0.0f;
            if (g > 1.0f) g = 1.0f;
            if (b < 0.0f) b = 0.0f;
            if (b > 1.0f) b = 1.0f;
        }

        public void pomnoz(float value)
        {
            if (value > 1.0f || value < 0.0f)
            {
                throw new Exception("Nieprawidłowy parametr funkcji. Należy podać wartość z zakresu 0.0-1.0");
            }
            else
            {
                this.r *= value;
                this.g *= value;
                this.b *= value;
            }

        }

        
        public uint toUint()
        {
            int _r, _g, _b, _a;
            _r = (int) (r * 255);
            _g = (int) (g * 255);
            _b = (int) (b * 255);
            _a = (int) (a * 255);

            return ((((uint) _a) << 24  |
                     ((uint)_r) << 16) |
                     (((uint)_g) << 8) |
                     ((uint)_b));

        }
        
        public override string ToString()
        {
            return "(" + this.r + ";" + this.g + ";" + this.b + ";" + this.a + ")";
        }

        public static Kolor operator *(Kolor left, Kolor right)
        {
            float r = left.R * right.R;
            if (r > 1.0f) { r = 1.0f; }
            if (r < 0.0f) { r = 0.0f; }
            float g = left.G * right.G;
            if (g > 1.0f) { g = 1.0f; }
            if (g < 0.0f) { g = 0.0f; }
            float b = left.B * right.B;
            if (b > 1.0f) { b = 1.0f; }
            if (b < 0.0f) { b = 0.0f; }
            return new Kolor(r, g, b);
        }

        public static Kolor operator +(Kolor left, Kolor right)
        {
            return new Kolor(left.R + right.R, left.G + right.G, left.B + right.B);
        }

        public Kolor mul(float c)
        {
            return new Kolor(this.R * c, this.G * c, this.B * c);
        }
    }
}
